package com.jt.tenant.conn;

import com.jt.tenant.app.JTApplication;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

@HttpInlet(Conn.INLET_GET_BATTERY_INFO)
public class GetBatteryInfo extends JTAsyGet {

    public String token = JTApplication.jtPreferences.readToken();

    public GetBatteryInfo(AsyCallBack asyCallBack) {

        super(asyCallBack);

    }

    @Override
    protected Info parserBody(Object body) {

        Info info = new Info();

        info.per = ((JSONObject) body).optString("per");

        info.remainTime = ((JSONObject) body).optString("remainTime");

        info.flameoutLastTime = ((JSONObject) body).optString("flameoutLastTime");

        info.batteryRemain = ((JSONObject) body).optString("batteryRemain");

        info.batteryPie = ((JSONObject) body).optString("batteryPie");

        info.batteryStatus = ((JSONObject) body).optString("batteryStatus");

        JSONArray array = ((JSONObject) body).optJSONArray("timeList");

        if(array != null){

            for (int i = 0 ;i < array.length(); i++){

                JSONObject o = array.optJSONObject(i);

                Voltage voltage = new Voltage();

                voltage.voltageRest = o.optString("voltageRest");

                voltage.voltageTime = o.optString("voltageTime");

                voltage.voltageId = o.optString("voltageId");

                info.list.add(voltage);

            }

        }

//        info.voltageRest = ((JSONObject) body).optString("voltageRest");

        return info;
    }

    public static class Info {

        public String per, remainTime, flameoutLastTime, batteryRemain, batteryPie, batteryStatus, voltageRest;

        public List<Voltage> list = new ArrayList<>();

    }

    public static class Voltage{

        public String voltageRest, voltageTime, voltageId;

    }

}
